/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { keyBy, get } from "lodash-es";
import { deleteByIds } from "@/plugins/ai-warning/organization/api/rdOrganizeAreaStand.ts";
import { rdOrganizeAreaStandStatusDictData } from "./common.tsx";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const rdOrganizeAreaStandStatusMap = keyBy(
    rdOrganizeAreaStandStatusDictData(),
    "value"
  );
  const msg = useMessage();

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    { 
      label: "所属工区", 
      prop: "area.name",
      showOverflowTooltip: true,
      cellRender: ({ row }) => {
        return row.area?.name || '-'
      }
    },
    { label: "站点名称", prop: "name", showOverflowTooltip: true },
    { label: "简称", prop: "abbr", showOverflowTooltip: true },
    { label: "地址", prop: "addr", showOverflowTooltip: true, width: 200 },
    { 
      label: "坐标", 
      prop: "coordinate",
      width: 150,
      cellRender: ({ row }) => {
        if (row.latitude && row.longitude) {
          return `${row.latitude}, ${row.longitude}`
        }
        return '-'
      }
    },
    {
      label: "状态",
      prop: "status",
      width: 100,
      align: "center",
      cellRender: ({ row }) => {
        const statusInfo = get(rdOrganizeAreaStandStatusMap, row.status, { label: "未知", value: row.status })
        const tagType = row.status === 1 ? 'success' : row.status === 2 ? 'danger' : 'info'
        return (
          <ElTag type={tagType}>
            {statusInfo.label}
          </ElTag>
        );
      },
    },
    { label: "备注", prop: "remark", showOverflowTooltip: true, width: 150 },
    {
      type: "operation",
      label: () => t("crud.operation"),
      fixed: "right",
      width: 120,
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) =>
              hasAuth("organization:rdOrganizeAreaStand:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) =>
              hasAuth("organization:rdOrganizeAreaStand:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
